$(function() {

	//去结算
	$(".jsan").click(function() {
		var count = $(".goods-item:checked").length;
		if (count < 1) {
			layer.msg("请选择要购买的商品");
			return;
		}
		var ids = "";
		$('.goods-item:checked').each(function() {
			var i = $(this).val();
			ids += i + ",";
		});
		var reg = /,$/gi;
		ids = ids.replace(reg, "");
		var target = encodeURI("./order.html?ids=" + ids);
		location.href = target;
	})

	//删除购物车商品
	$("#car_goods_item").on("click", ".remove_car", function() {
		var itemId = $(this).attr("data-item-id");
		var carId = $(this).attr("data-car-id");
		var goodsId = $(this).attr("data-goods-id");
		var node = $("#goods_item_container_" + goodsId);
		$.ajax({
			type: "POST",
			url: "http://127.0.0.1:8080/goodsCar/deleteCarItem",
			dataType: "json",
			data: {
				"itemId": itemId,
				"carId": carId
			},
			success: function(data) {
				if (data.code == 'success') {
					node.remove();
				}
			}
		});
	})

	//获取登陆用户session
	$.ajax({
		type: "POST",
		url: "http://127.0.0.1:8080/user/getSession",
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(data) {
			var code = data.code;
			if (code != 'success') {
				window.location.href = './login.html';
			}
		}
	});

	//购物车商品数量变更
	$("#car_goods_item").on("blur", ".shuliang", function() {
		var count = $(this).val();
		var goodsId = $(this).attr("data-id");
		$.ajax({
			type: "POST",
			url: "http://127.0.0.1:8080/goodsCar/changeGoodsNumber",
			dataType: "json",
			data: {
				"goodsId": goodsId,
				"count": count
			},
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				if (data.code == 'success') {
					var price = $("#price_" + goodsId).text();
					var total = parseInt(price) * parseInt(count);
					$("#total_money_" + goodsId).text(total);
				}
			}
		});
	});

	//获取购物车数据
	$.ajax({
		type: "POST",
		url: "http://127.0.0.1:8080/goodsCar/getCarGoods",
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(data) {
			if (data.code == 'success') {
				var node = $("#car_goods_item");
				var item = data.data;
				var c = item.length;
				$("#goods_count").text(c);
				var html = "";
				item.forEach(function(goods) {
					var specificationStr = "";
					var specification = goods.goods.specification;
					if (specification == 1) {
						specificationStr = "全网通版 6GB+64GB";
					} else if (specification == 2) {
						specificationStr = "全网通版 6GB+128GB";
					}
					var count = goods.count;
					var price = goods.goods.price;
					var totalMoney = parseInt(count) * parseInt(price);
					html += '<div class="content2 center" id="goods_item_container_' + goods.goods.id +
						'"><div class="sub_content fl"><input type="checkbox" value="' + goods.goods.id +
						'" class="goods-item"/></div><div class="sub_content fl">' +
						'<img src="' + goods.goods.imgUrl + '" height="90" width="90"></div>' +
						'<div class="sub_content fl ft20">' + goods.goods.goodsName + '</div>' +
						'<div class="sub_content fl" id="price_' + goods.goods.id + '">' + price +
						'</div><div class="sub_content fl"><input class="shuliang" type="number"' +
						' value="' + count + '" step="1" min="1" data-id="' + goods.goods.id +
						'"></div><div class="sub_content fl" id="total_money_' + goods.goods.id +
						'">' + totalMoney + '</div><div class="sub_content fl"><a href="#" ' +
						'class="remove_car" data-item-id="' + goods.itemId +
						'" data-car-id="' + goods.carId + '" data-goods-id="' + goods.goods.id +
						'">×</a></div><div class="clear"></div></div>';
				})
				node.html(html)
			}
		}
	});

	//单个选中
	$("#car_goods_item").on("click", ".goods-item", function() {
		var count = $(".goods-item:checked").length;
		$("#select_count").text(count);
		var chk_value = [];
		$('#car_goods_item .goods-item:checked').each(function() {
			var i = $(this).val();
			var content = $("#total_money_" + i).text();
			chk_value.push(content);
		});
		var total = 0;
		chk_value.forEach(function(obj) {
			total += parseInt(obj);
		})
		$("#totalAmount").text(total + "元");
	})

	//全部选择
	$("#checkAll").click(function() {
		if (this.checked) {
			$("#car_goods_item .goods-item").attr('checked', true);
		} else {
			$("#car_goods_item .goods-item").attr('checked', false);
		}
		var chk_value = [];
		$('#car_goods_item .goods-item:checked').each(function() {
			var i = $(this).val();
			var content = $("#total_money_" + i).text();
			chk_value.push(content);
		});
		var total = 0;
		chk_value.forEach(function(obj) {
			total += parseInt(obj);
		})
		$("#totalAmount").text(total + "元");
		var count = $(".goods-item:checked").length;
		$("#select_count").text(count);
	})
})
